﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileSales.Website.EmployeeService;
using MobileSales.Model.Employees;
using System.ServiceModel;
using MobileSales.Model.Exceptions;
using MobileSales.Common.Logger;
using MobileSales.Website.Infrastructure;
using MobileSales.Website.Localization;

namespace MobileSales.Website.Controllers
{
    [Authorize]
    public class MessagesController : ControllerBase
    {
        #region Index
        //
        // GET: /Messages/

        public ActionResult Index()
        {
            EmployeeServiceClient employeeService = new EmployeeServiceClient();
            employeeService.AddValidationToken(this.Token);
            Message[] messages = employeeService.GetMessageList(this.EmployeeId);
            employeeService.Close();
            if (messages == null)
                this.SetMessage(ErrorStrings.ErrorMessageList, Infrastructure.Messaging.MessageType.Error);
            return View(messages);
        } 
        #endregion

        #region Read
        //
        // GET: /Messages/Read/5

        public ActionResult Read(int id)
        {
            EmployeeServiceClient employeeService = new EmployeeServiceClient();
            employeeService.AddValidationToken(this.Token);
            Message message = employeeService.GetMessage(id);
            if (message != null)
            {
                try
                {
                    employeeService.ReadMessage(message.Id);
                }
                catch(Exception ex)
                {
                    CLogger.LogDebug(ex);
                    message = null;
                    this.SetMessage(ErrorStrings.ErrorMessage, Infrastructure.Messaging.MessageType.Error);
                }
            }
            else
            {
                this.SetMessage(ErrorStrings.ErrorMessage, Infrastructure.Messaging.MessageType.Error);
            }
            employeeService.Close();

            return View(message);
        } 
        #endregion

        #region Compose
        //
        // GET: /Messages/Create

        public ActionResult Compose()
        {
            return View();
        }

        //
        // POST: /Messages/Create

        [HttpPost]
        public ActionResult Compose(Message message)
        {
            try
            {
                EmployeeServiceClient employeeService = new EmployeeServiceClient();
                employeeService.AddValidationToken(this.Token);
                employeeService.SendMessage(this.EmployeeId, message.RecepientId, message.Subject, message.Content);
                employeeService.Close();
                this.SetMessage(InfoStrings.MessageSent, Infrastructure.Messaging.MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, Infrastructure.Messaging.MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorMessageSending, Infrastructure.Messaging.MessageType.Error);

            }
            return View(message);
        } 
        #endregion

        #region Delete
        //
        // GET: /Messages/Delete/5

        public ActionResult Delete(int id)
        {
            return Read(id);
        }

        //
        // POST: /Messages/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                EmployeeServiceClient employeeService = new EmployeeServiceClient();
                employeeService.AddValidationToken(this.Token);
                employeeService.DeleteMessage(id);
                employeeService.Close();
                this.SetMessage(InfoStrings.MessageDeleted, Infrastructure.Messaging.MessageType.Info);
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, Infrastructure.Messaging.MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorMessageDeleting, Infrastructure.Messaging.MessageType.Error);

            }
            return RedirectToAction("Index");
        } 
        #endregion
    }
}
